package v1

import (
	"eci-forward/pkg/eci"
	"eci-forward/pkg/logs"

	"github.com/gin-gonic/gin"
)

func ECICreateImageCache(c *gin.Context) {
	images := c.PostFormArray("images")
	if len(images) == 0 {
		c.JSON(400, gin.H{
			"message": "images can not be empty",
		})
		return
	}
	imageCacheName, err := eci.CreateImageCache(&images)
	if err != nil {
		c.JSON(500, gin.H{
			"message":          err.Error(),
			"image_cache_name": "",
		})
		return
	}
	c.JSON(200, gin.H{
		"message":          "success",
		"image_cache_name": imageCacheName,
	})
}

func ECIDescribeImageCaches(c *gin.Context) {
	imageCacheName := c.Query("image_cache_name")
	if imageCacheName == "" {
		logs.Logger.Info("image_cache_name is empty, describe all image caches")
	}
	res, err := eci.DescribeImageCachesWithImageCacheName(imageCacheName)
	if err != nil {
		c.JSON(500, gin.H{
			"message": err.Error(),
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"message": "success",
		"data":    res,
	})
}
